package com.zzyweb.rentalcarsys.controller;

import com.zzyweb.rentalcarsys.entity.RentalOrder;
import com.zzyweb.rentalcarsys.service.OrderService;
import com.zzyweb.rentalcarsys.utils.PageResult;
import com.zzyweb.rentalcarsys.utils.ResponseObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhiyi
 * @date 10/10/25
 **/
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderService orderService;
    @GetMapping("/all")
    ResponseObject getAllOrders(@RequestParam Long currentPage, Long pageSize) {
        PageResult<RentalOrder> orders = orderService.getAllOrders(currentPage, pageSize);
        return ResponseObject.ok(orders);
    }

    @PostMapping("/conditions")
    ResponseObject getAllByConditions(@RequestParam Long currentPage, @RequestParam Long pageSize,@RequestBody RentalOrder order){
        System.out.println(order);
        PageResult<RentalOrder> orders = orderService.getOrdersByConditions(currentPage, pageSize, order);
        return ResponseObject.ok(orders);
    }
    @PutMapping("/modify")
    ResponseObject modifyOrder(@RequestBody RentalOrder order) {
        orderService.modifyOrder(order);
        return ResponseObject.ok();
    }

    @PostMapping("/add")
    ResponseObject addOrder(@RequestBody RentalOrder order) {
        orderService.addOrder(order);
        return ResponseObject.ok();
    }
    @GetMapping("/allByUserId/{userId}")
    ResponseObject getAllByUserId(@PathVariable String userId, @RequestParam Long currentPage, @RequestParam Long pageSize) {
        PageResult<RentalOrder> orders = orderService.getOrdersByUserId(userId, currentPage, pageSize);
        return ResponseObject.ok(orders);
    }
    @DeleteMapping("/delete/{orderId}")
    ResponseObject deleteOrder(@PathVariable String orderId) {
        orderService.deleteOrder(orderId);
        return ResponseObject.ok();
    }
    @PutMapping("/cancel")
    ResponseObject cancelOrder(@RequestBody RentalOrder order) {
        orderService.cancelOrder(order);
        return ResponseObject.ok();
    }
    @PutMapping("/return")
    ResponseObject returnCar(@RequestBody RentalOrder order) {
        RentalOrder rentalOrder = orderService.returnCar(order);
        return ResponseObject.ok(rentalOrder);
    }

    @PutMapping("/review")
    ResponseObject reviewOrder(@RequestBody RentalOrder order) {
        return ResponseObject.ok(orderService.reviewOrder(order));
    }
}
